from selenium import webdriver
from appium import webdriver as appdriver
import time


class DriverUtil:
    __mp_driver = None  # 自媒体
    __mis_driver = None  # 后台
    __app_driver = None  # app 端

    @classmethod
    def get_mp_driver(cls):
        """获取自媒体端的 driver"""
        if cls.__mp_driver is None:
            cls.__mp_driver = webdriver.Chrome()
            cls.__mp_driver.maximize_window()

            cls.__mp_driver.implicitly_wait(10)

        return cls.__mp_driver

    @classmethod
    def quit_mp_driver(cls):
        """退出 自媒体端的 driver"""
        if cls.__mp_driver:
            time.sleep(3)
            cls.__mp_driver.quit()
            cls.__mp_driver = None

    @classmethod
    def get_mis_driver(cls):
        """后台的 driver"""
        if cls.__mis_driver is None:
            cls.__mis_driver = webdriver.Firefox()
            cls.__mis_driver.maximize_window()
            cls.__mis_driver.implicitly_wait(10)

        return cls.__mis_driver

    @classmethod
    def quit_mis_driver(cls):
        """退出后台的driver"""
        if cls.__mis_driver:
            time.sleep(3)
            cls.__mis_driver.quit()
            cls.__mis_driver = None

    @classmethod
    def get_app_driver(cls):
        """app driver"""
        if cls.__app_driver is None:
            # com.itcast.toutiaoApp/.MainActivity
            desired_capabilities = {
                "platformName": "android",
                "platformVersion": "6",
                "deviceName": "HuaWei",
                "appPackage": "com.itcast.toutiaoApp",
                "appActivity": ".MainActivity"
            }
            cls.__app_driver = appdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_capabilities)
            cls.__app_driver.implicitly_wait(10)
        return cls.__app_driver

    @classmethod
    def quit_app_driver(cls):
        """退出 app driver"""
        if cls.__app_driver:
            time.sleep(3)
            cls.__app_driver.quit()
            cls.__app_driver = None


if __name__ == '__main__':
    DriverUtil.get_mp_driver().get('http://pc-toutiao-python.itheima.net/#/login')
    time.sleep(2)
    DriverUtil.quit_mp_driver()
    DriverUtil.get_mis_driver().get('http://ttmis.research.itcast.cn/')
    time.sleep(2)
    DriverUtil.quit_mis_driver()
    DriverUtil.get_app_driver()
    time.sleep(2)
    DriverUtil.quit_app_driver()